package com.edu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * @Author:zyn
 * @Date:2025/11/14--13:52:59
 */
@Aspect
@Component
public class MyAdvise {

    private Logger logger= LoggerFactory.getLogger(MyAdvise.class);

    @Pointcut("execution( * com.edu.mapper..*.insert*(..)) || " +
              "execution(* com.edu.mapper..*.delete*(..)) ||" +
              "execution(* com.edu.mapper..*.update*(..))")
    public void pt1(){

    }
    @Before("pt1()")
    public void logBeforeAdvise(JoinPoint jp) {
        logger.debug(jp.getTarget() + ":" + jp.getSignature().getName() + "()方法执行开始于：" + new Date());
    }

    @Around("execution(* com.edu.mapper..*.select*(..))")
    public Object logAroundAdvise(ProceedingJoinPoint pjp){
        Object result=null;
            try {
                long begin=System.currentTimeMillis();
                result=pjp.proceed();
                Thread.sleep(1000);
                long end=System.currentTimeMillis();
                logger.debug(pjp.getTarget()+":"+pjp.getSignature().getName()+"()方法执行耗时："+(end-begin)+"毫秒");
            } catch (Throwable e) {
                e.printStackTrace();
            }
        return result;
    }
}
